#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "qcustomplot.h"
#include "custombar.h"
#include "server.h"
#include <QTcpServer>
#include <QTcpSocket>
#include <QScreen>
#include "controlform.h"

class QToolButton;

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

    void CustomBar_car();
    void CustomBar_dg();
    void CustomBar_sys();
    void CustomBar_System();
    void CustomBar_nb1();
    void CustomBar_nb2();
    void CustomBar_nb3();
    void CustomBar_nbtem();
    void CustomBar_nbW();
    void CustomBar_DC();
    void CustomBar_Charge();
    void CustomBar_JS();
    void CustomBar_Js_Sys();
    void CustomBar_DG();
    void CustomBar_Bug();
    void InitTableDG();
    void InitTableBug();
    void signCar1(int num);

    //车载TCP连接显示函数
    void TCPCar(bool bo);
    //导轨TCP连接显示函数
    void TCPDG();

    //导轨数据显示函数
    void DG1View(QString da);
    void DG2View(QString da);
    void DG3View(QString da);
    void DG4View(QString da);
    void DG5View(QString da);
    void DG6View(QString da);
    void DG7View(QString da);
    void DG8View(QString da);
    void DG9View(QString da);
    bool trackcut = true;
    float trackV1 = 0;
    float trackA1 = 0;
    float trackX1 = 0;
    float trackTemM1 = 0;//磁芯
    float trackTemC1 = 0;//电容

    //DCDC数据解析
    void DCDC1DATA(QString da);
    QString dcbug1_1 = "0";
    QString dcbug1_2 = "0";
    QString dcbug1_3 = "0";
    QString dcbug1_4 = "0";
    QString dcbug1_5 = "0";
    QString dcbug1_6 = "0";
    int DC1_inV = 0;
    int DC1_inA = 0;
    int DC1_inW = 0;
    int DC1_putV = 0;
    int DC1_putA = 0;
    int DC1_putW = 0;
    int ZLQ1_inV = 0;
    int ZLQ1_inA = 0;
    int SQ1Tem = 0;
    int XQ1Tem = 0;
    int DG1Tem = 0;

    //定位信息解析
    void LocationView(QString da);
    //导轨开关控制限值
    int dg1_min = 20;
    int dg1_max = 60;
    int dg2_min = 20;
    int dg2_max = 100;
    int dg3_min = 20;
    int dg3_max = 140;
    int dg4_min = 20;
    int dg4_max = 180;
    int dg5_min = 20;
    int dg5_max = 220;
    int dg6_min = 20;
    int dg6_max = 260;
    int dg7_min = 20;
    int dg7_max = 300;
    int dg8_min = 20;
    int dg8_max = 340;
    int dg9_min = 20;
    int dg9_max = 380;

    //车载温度信息解析
    void CarTemView(QString da);


    QByteArray hexStringToByteArray(QString HexString);
    quint16 calculateCRC16(const QByteArray &data);

    void timerEvent(QTimerEvent *event);

    bool bol1 = false;
    bool bol2 = false;
    bool bol3 = false;
    bool bol4 = false;
    bool bol5 = false;
    bool bol6 = false;
    bool bol7 = false;
    bool bol8 = false;
    bool bol9 = false;

    //导轨TCP心跳包计数
    int count1 = 0;
    int count2 = 0;
    int count3 = 0;
    int count4 = 0;
    int count5 = 0;
    int count6 = 0;
    int count7 = 0;
    int count8 = 0;
    int count9 = 0;

    //访问PLC数据
    QString dataQ = "01 01 00 00 00 08";
    QString dataI = "01 02 00 00 00 08";
    QString dataD = "01 03 00 00 00 03";
    QString dataW = "01 04 00 08 00 04";
    QString dataK = "01 06 00 0A 00 01";
    QString dataC = "01 06 00 0A 00 00";


private slots:
    //获取本机IP地址
    QString getLocalIP();

    //界面初始化函数
    void initForm();
    void buttonClick();
    void buttonClick1();
    void initLeftMain();
    void initLeftConfig();
    void leftMainClick();
    void leftConfigClick();

    //界面按钮函数
    void on_btnMenu_Min_clicked();
    void on_btnMenu_Max_clicked();
    void on_btnMenu_Close_clicked();
    void on_btnHelp_4_clicked();

/****************** TCP通讯函数 ****************/
    //连接函数
    void mNewConnection1();
    void mNewConnection2();
    void mNewConnection3();
    void mNewConnection4();
    void mNewConnection5();
    void mNewConnection6();
    void mNewConnection7();
    void mNewConnection8();
    void mNewConnection9();
    //断开函数
    void onDisconnected1();
    void onDisconnected2();
    void onDisconnected3();
    void onDisconnected4();
    void onDisconnected5();
    void onDisconnected6();
    void onDisconnected7();
    void onDisconnected8();
    void onDisconnected9();
    //接收函数
    void receiveMessage1();
    void receiveMessage2();
    void receiveMessage3();
    void receiveMessage4();
    void receiveMessage5();
    void receiveMessage6();
    void receiveMessage7();
    void receiveMessage8();
    void receiveMessage9();
    //心跳包函数
    void doheart1();
    void doheart2();
    void doheart3();
    void doheart4();
    void doheart5();
    void doheart6();
    void doheart7();
    void doheart8();
    void doheart9();

/****************** PLC数据访问函数 ****************/
    //读取PLC输出状态Q
    void sendDataQ1();
    void sendDataQ2();
    void sendDataQ3();
    void sendDataQ4();
    void sendDataQ5();
    void sendDataQ6();
    void sendDataQ7();
    void sendDataQ8();
    void sendDataQ9();
    //读取PLC输入状态I
    void sendDataI1();
    void sendDataI2();
    void sendDataI3();
    void sendDataI4();
    void sendDataI5();
    void sendDataI6();
    void sendDataI7();
    void sendDataI8();
    void sendDataI9();
    //读取导轨电压、电流、相位等数据
    void sendDataD1();
    void sendDataD2();
    void sendDataD3();
    void sendDataD4();
    void sendDataD5();
    void sendDataD6();
    void sendDataD7();
    void sendDataD8();
    void sendDataD9();
    //读取导轨温度数据
    void sendDataW1();
    void sendDataW2();
    void sendDataW3();
    void sendDataW4();
    void sendDataW5();
    void sendDataW6();
    void sendDataW7();
    void sendDataW8();
    void sendDataW9();
    //发射导轨开关控制
    void sendDataK1();
    void sendDataK2();
    void sendDataK3();
    void sendDataK4();
    void sendDataK5();
    void sendDataK6();
    void sendDataK7();
    void sendDataK8();
    void sendDataK9();
    void sendDataK10();
    void sendDataC1();
    void sendDataC2();
    void sendDataC3();
    void sendDataC4();
    void sendDataC5();
    void sendDataC6();
    void sendDataC7();
    void sendDataC8();
    void sendDataC9();
    void sendDataC10();

signals:
    //PLC数据信号槽
    void sig_track1Data(QString da);
    void sig_track2Data(QString da);
    void sig_track3Data(QString da);
    void sig_track4Data(QString da);
    void sig_track5Data(QString da);
    void sig_track6Data(QString da);
    void sig_track7Data(QString da);
    void sig_track8Data(QString da);
    void sig_track9Data(QString da);

    void tcpcon();
    void tcpdis();

private:
    Ui::MainWindow *ui;

    ControlForm *conform;

    QList<int> pixCharMain;
    QList<QToolButton *> btnsMain;

    QList<int> pixCharConfig;
    QList<QToolButton *> btnsConfig;

    CustomBar *Send1;
    QVector<double> ticks1;

    CustomBar *Send2;
    QVector<double> ticks2;

    CustomBar *nbtem1;
    CustomBar *nbtem2;
    QVector<double> ticks3;

    CustomBar *DC1;
    QVector<double> ticks4;

    CustomBar *Charge1;
    QVector<double> ticks5;

    CustomBar *JS1;
    QVector<double> ticks6;

    QCPBars *DG1;
    QCPBars *DG2;
    QVector<double> ticks7;

    Server *serverCar = NULL;
    QTcpServer *tcpServer1 = NULL;
    QTcpServer *tcpServer2 = NULL;
    QTcpServer *tcpServer3 = NULL;
    QTcpServer *tcpServer4 = NULL;
    QTcpServer *tcpServer5 = NULL;
    QTcpServer *tcpServer6 = NULL;
    QTcpServer *tcpServer7 = NULL;
    QTcpServer *tcpServer8 = NULL;
    QTcpServer *tcpServer9 = NULL;
    QTcpSocket *tcpsocket1 = NULL;
    QTcpSocket *tcpsocket2 = NULL;
    QTcpSocket *tcpsocket3 = NULL;
    QTcpSocket *tcpsocket4 = NULL;
    QTcpSocket *tcpsocket5 = NULL;
    QTcpSocket *tcpsocket6 = NULL;
    QTcpSocket *tcpsocket7 = NULL;
    QTcpSocket *tcpsocket8 = NULL;
    QTcpSocket *tcpsocket9 = NULL;

    QTimer *m_timer1 = NULL;
    QTimer *m_timer2 = NULL;
    QTimer *m_timer3 = NULL;
    QTimer *m_timer4 = NULL;
    QTimer *m_timer5 = NULL;
    QTimer *m_timer6 = NULL;
    QTimer *m_timer7 = NULL;
    QTimer *m_timer8 = NULL;
    QTimer *m_timer9 = NULL;

    QTimer *h_timer1 = NULL;
    QTimer *h_timer2 = NULL;
    QTimer *h_timer3 = NULL;
    QTimer *h_timer4 = NULL;
    QTimer *h_timer5 = NULL;
    QTimer *h_timer6 = NULL;
    QTimer *h_timer7 = NULL;
    QTimer *h_timer8 = NULL;
    QTimer *h_timer9 = NULL;

    int refreshTimer; //刷新图像的定时器
    int sampleTimer; //模拟采样的定时器
};
#endif // MAINWINDOW_H
